package com.sucisoft.base;

import android.app.Application;
import android.content.Context;
import android.content.res.Resources;

import com.sucisoft.base.application.AppAbstract;
import com.sucisoft.base.imageloder.config.ImgC;
import com.sucisoft.base.imageloder.interfaces.ImageBase;
import com.sucisoft.base.network.callback.ICallback;
import com.sucisoft.base.network.config.NetC;
import com.sucisoft.base.network.interfaces.IHttpBase;

import java.util.ArrayList;
import java.util.List;

/**
 * 2033152950
 * Created by zf on 2017/12/19.
 */

public class Helper extends AppAbstract implements ImageBase, IHttpBase {

    private static Helper helper;

    private static Context context;

    private static Application application;

    private static ImageBase ImageBase = null;

    private static IHttpBase mIhttpBase = null;

    // #log
    public static String tag = "---log---";

    public static boolean isDebug = true;

    private Helper() {
    }

    public static Context getContext() {
        return context;
    }

    public static Application getApplication() {
        return application;
    }

    public static Resources getResources() {
        return context.getResources();
    }

    private static List<AppAbstract> appAbstracts = new ArrayList<AppAbstract>();

    public static Helper instance(Context context, Application application) {
        if (helper == null) {
            synchronized (Helper.class) {
                if (helper == null) {
                    helper = new Helper();
                }
            }
        }
        Helper.context = context;
        Helper.application = application;
        return helper;
    }

    public static Helper obtian() {
        return helper;
    }

    public static void addImage(ImageBase imageBase) {
        if (ImageBase == null)
            ImageBase = imageBase;
    }


    public static void addHttp(IHttpBase ihttpBase) {
        if (mIhttpBase == null)
            mIhttpBase = ihttpBase;
    }

    public static Helper addHelper(AppAbstract appHelper) {
        if (appAbstracts == null) {
            appAbstracts = new ArrayList<>();
        }
        if (!appAbstracts.contains(appHelper)) {
            appAbstracts.add(appHelper);
        }
        return helper;
    }

    @Override
    public void attach() {
        for (AppAbstract appAbstract : appAbstracts) {
            appAbstract.attach();
        }
    }

    @Override
    public void init() {
        for (AppAbstract appAbstract : appAbstracts) {
            appAbstract.init();
        }
    }


    @Override
    public void load(ImgC config) {
        if (ImageBase == null) {
            throw new NullPointerException("Image Helper is null");
        }
        ImageBase.load(config);
    }

    @Override
    public void PostCall(NetC config, ICallback callback) {
        if (mIhttpBase == null) {
            throw new NullPointerException("Http Helper is null");
        }
        mIhttpBase.PostCall(config, callback);
    }

    @Override
    public void GetCall(NetC config, ICallback callback) {
        if (mIhttpBase == null) {
            throw new NullPointerException("Http Helper is null");
        }
        mIhttpBase.GetCall(config, callback);
    }

    @Override
    public void cancel(Object tag) {
        if (mIhttpBase == null) {
            return;
        }
        mIhttpBase.cancel(tag.getClass().getName());
    }

}
